package com.whut.whw.message;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

import android.support.design.widget.TabLayout;
import com.whut.whw.R;
import com.whut.whw.base.BaseFragment;
import com.whut.whw.chat.ChatListFragment;

/**
 * <pre>
 *  desc: 消息，包括物友、@我、关注、系统通知等
 *  Created by 忘尘无憾 on 2017/06/13.
 *  version:
 * </pre>
 */
public class MsgFragment extends BaseFragment {

    public static MsgFragment newInstance() {
        return new MsgFragment();
    }

    private Toolbar msg_toolbar;
    private TabLayout msg_tab_top;
    private ViewPager msg_viewpager;
    private FragmentPagerAdapter mAdapter;

    @Override
    public View initView(LayoutInflater inflater) {
        View rootView = inflater.inflate(R.layout.fragment_msg, null);
        msg_toolbar = (Toolbar) rootView.findViewById(R.id.msg_toolbar);
        msg_tab_top = (TabLayout) rootView.findViewById(R.id.msg_tab_top);
        msg_viewpager = (ViewPager) rootView.findViewById(R.id.msg_viewpager);
        return rootView;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        initToolBar();
        initViewPager();
        initTabLayout();
    }

    /**
     * 初始化toolbar标题、菜单
     */
    private void initToolBar() {
        msg_toolbar.setTitle(getResources().getString(R.string.msg_title));
        msg_toolbar.inflateMenu(R.menu.fragment_msg_toolbar_menu);
    }

    /**
     * 初始化tab
     * 包括有物友、@我、关注、通知
     */
    private void initTabLayout() {
        msg_tab_top.setupWithViewPager(msg_viewpager);//绑定viewpager，使之可联动
        msg_tab_top.getTabAt(0).setText(getResources().getString(R.string.msg_tab_chat));
        msg_tab_top.getTabAt(1).setText(getResources().getString(R.string.msg_tab_comment));
        msg_tab_top.getTabAt(2).setText(getResources().getString(R.string.msg_tab_follow));
        msg_tab_top.getTabAt(3).setText(getResources().getString(R.string.msg_tab_like));

    }

    /**
     * 初始化viewpage，给viewpager填充各个fragment
     */
    private void initViewPager() {

        final Fragment[] fragments = new Fragment[]{
                ChatListFragment.newInstance(),
                MsgMeFragment.newInstance(),
                MsgFollowFragment.newInstance(),
                MsgFollowFragment.newInstance()
        };
        mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        };
        msg_viewpager.setAdapter(mAdapter);
    }

    @Override
    public void setListener() {


    }
}
